#include <stdio.h>
#include <windows.h>

void Swap(int* left, int* right)
{
    int tmp = * left;
    *left = *right;
    *right = tmp;
}

void Print(int *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int partion(int* arr, int low, int high)
{
    int pivot = arr[high];
    int i = low;
    int j = low;

    while(j < high)
    {
        if(arr[j] < pivot)
        {
            if(arr[i] != arr[j] )
                Swap(&arr[i], &arr[j]);
            i++;
            j++;
        }

        else
            j++;
    }

    if(arr[i] != arr[j] )
        Swap(&arr[i], &arr[j]);
    return i;

}

void QSort(int* arr, int left, int right)
{
    //如果序列中只有一个数，则是有序的
    if(left >= right)
        return;
    
    else
    {
        int mid = partion(arr, left, right);

        QSort(arr, left, mid - 1);

        QSort(arr, mid + 1, right);
    }
}

void quick_sort(int* arr, int size)
{
    QSort(arr, 0, size - 1);
}


int main()
{
    
    int arr[10] = {9,6,8,4,2,5,7,1,3,10};
    int size = sizeof(arr) / sizeof(arr[0]);
    quick_sort(arr, size);
    Print(arr, size);
    system("pause");
    return 0;
}